﻿#include <iostream>
#include <vector>
using namespace std;


class Solution
{
public:
    // 416. 分割等和子集
    /*bool canPartition(vector<int>& nums)
    {
        int n = nums.size(), sum = 0;
        for (auto num : nums)
            sum += num;
        if (sum % 2 == 1)
            return false;

        int V = sum / 2;
        vector<vector<bool>> dp(n + 1, vector<bool>(V + 1));
        for (int i = 0; i <= n; i++)
            dp[i][0] = true;

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= V; j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i - 1])
                    dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];
            }

        return dp[n][V];
    }*/

    // 优化
    bool canPartition(vector<int>& nums)
    {
        int n = nums.size(), sum = 0;
        for (auto num : nums)
            sum += num;
        if (sum % 2 == 1)
            return false;

        int V = sum / 2;
        vector<bool> dp(V + 1);
        dp[0] = true;

        for (int i = 1; i <= n; i++)
            for (int j = V; j >= nums[i - 1]; j--)
                dp[j] = dp[j] || dp[j - nums[i - 1]];

        return dp[V];
    }
};

int main()
{

    return 0;
}